package com.zprmb.udzt;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zprmb.udzt.bean.Mycoupon;
import com.zprmb.udzt.bean.Red_CouponBean;
import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.util.BitmapUtils;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.Regular;
import com.zprmb.udzt.util.ToastUtil;
import com.zprmb.udzt.util.VolleryUtil;

import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zprmb.udzt.R.id.tv_reward_name;

/**
 * 投资优惠券
 *
 * @author shenshao
 */
public class Investcoupon extends BaseActivity implements OnClickListener {
    private ListView lv;
    private int imgdata = 0, imgamount = 0;
    private CheckBox checkBox;
    private TextView tv_v4_pay_redpackage, tv_use_red, tv_btn;
    private ArrayList<Red_CouponBean> list = new ArrayList<Red_CouponBean>();
    private UseCoupon adapter;
    private ImageView img_back;
    private RelativeLayout rl_back;
    private Map<Integer, Boolean> isSelected;
    private List beSelectedData = new ArrayList();
    private String coupon_id, rateadd, name, date, id, type, flag, add_type, availableGiftAmount, red_type;
    private CheckBox ck_red, ck_coupon;

    private String myid;
    private LinearLayout li_amount, li_data;
    private ImageView img_amount, img_data;
    private TextView text_amount, text_data;
    private String money, isred, isnew;
    private int month;
    private Button btn_confirm;
    private TextView tv_title;
    private double red, rate;
    private int contr = 0, kedian = 0;
    private int maxAmount = 0;
    private int minAmount = 0;
    private DecimalFormat df = new DecimalFormat("###0.00");
    private DecimalFormat df2 = new DecimalFormat("###");
    private DecimalFormat df3 = new DecimalFormat("###0.0");
    SharedPreferences sha;
    private int ok = 0;
    private LinearLayout li_red;
    private ImageView img_none_list;


    private String intentTitle;


    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        BbtApplication.getInstance().removeActvity(this);
    }

    String getMoney;
//    http://test1.udrmb.com//api/coupon/getUserCouponsNew/ASSET_COMMON?flagFresh=false&productId=2A125641-96D5-11E7-97CF-5254003C8227&sortType=byDate&sortFlag=0

    String zhekouMoney;
    String maxMOney;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.investcoupon);
        BbtApplication.getInstance().addActvity(this);
//		 setTitle("选择礼券",Investcoupon.this);
        setTitle("选择福利", Investcoupon.this);
//		 setgoback(Investcoupon.this);
        View lv_header = getLayoutInflater().inflate(R.layout.investcoupon_header, null);
        lv = (ListView) findViewById(R.id.lv);
        img_back = (ImageView) findViewById(R.id.img_back);
        tv_use_red = (TextView) findViewById(R.id.tv_keyong_red);
        tv_v4_pay_redpackage = (TextView) findViewById(R.id.with_money);
        ck_red = (CheckBox) findViewById(R.id.ck_red);

        img_none_list = (ImageView) findViewById(R.id.img_none_list);
        li_red = (LinearLayout) findViewById(R.id.li_red);
        btn_confirm = (Button) findViewById(R.id.btn_confirm);
        li_amount = (LinearLayout) findViewById(R.id.li_amount);
        li_data = (LinearLayout) findViewById(R.id.li_data);
        img_amount = (ImageView) findViewById(R.id.img_amount);
        img_data = (ImageView) findViewById(R.id.img_data);
        text_amount = (TextView) findViewById(R.id.tv_amount);
        text_data = (TextView) findViewById(R.id.tv_data);
        rate = getIntent().getDoubleExtra("rate", 0);
        money = getIntent().getStringExtra("money");
        red = getIntent().getDoubleExtra("red", 0);
        flag = getIntent().getStringExtra("flag");
        month = getIntent().getIntExtra("month", 0);
        tv_use_red.setText("(还有" + df.format(red) + "元红包可用)");
        id = getIntent().getStringExtra("id");
        type = getIntent().getStringExtra("type");
        isred = getIntent().getStringExtra("isred");
        isnew = getIntent().getStringExtra("isnew");
        sha = getSharedPreferences("coupon", MODE_PRIVATE);
        myid = sha.getString("check", "");
        lv.addHeaderView(lv_header);
//        Toast.makeText(this, getIntent().getIntExtra("amount", 0) +"", Toast.LENGTH_SHORT).show();
//		if(sha.getString("check", "").equals("")){
//			btn_confirm.setBackgroundResource(R.drawable.btn_unclick);
//			btn_confirm.setEnabled(false);
//			kedian=0;
//		}else{
//			btn_confirm.setBackgroundResource(R.drawable.my_button);
//			btn_confirm.setEnabled(true);
//			kedian=1;
//			add_type=sha.getString("type", "");
//			
//		}
        Log.e("kedian", sha.getString("check", "") + "id");
        li_data.setSelected(true);
        li_amount.setSelected(false);
        if (isred.equals("false")) {
            li_red.setBackgroundColor(Color.parseColor("#f0f0f0"));
            ck_red.setEnabled(false);
        } else {
            li_red.setBackgroundColor(Color.parseColor("#ffffff"));
            ck_red.setEnabled(true);
        }
        li_amount.setOnClickListener(this);
        li_data.setOnClickListener(this);
        ck_red.setOnClickListener(this);
        rl_back = (RelativeLayout) findViewById(R.id.rl_back);
        rl_back.setOnClickListener(this);
        btn_confirm.setOnClickListener(this);
        img_back.setOnClickListener(this);
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                // TODO Auto-generated method stub
                if (position != 0) {


                    if (list.get(position - 1).getType().equals("INTEREST_RATES")) {


                        if (getIntent().getIntExtra("amount", 0) > Double
                                .parseDouble(list.get(position - 1).getMaxAmount())
                                || getIntent().getIntExtra("amount", 0) < Double
                                .parseDouble(list.get(position - 1)
                                        .getMinAmount())) {
                            ToastUtil.showCenterToast(Investcoupon.this,
                                    "投资金额不在限投金额之内，重新选择福利或重新输入投资金额!");
                            return;
                        }
                    } else if (list.get(position - 1).getType().equals("REBATE")) {


                        if (getIntent().getIntExtra("amount", 0) < Double
                                .parseDouble(list.get(position - 1)
                                        .getMinAmount())) {
                            ToastUtil.showCenterToast(Investcoupon.this,
                                    "投资金额不在限投金额之内，重新选择福利或重新输入投资金额!");
                            return;
                        }

                    }

                    contr = 1;
                    ck_red.setChecked(false);
                    boolean cu = true;
//				if(isSelected.get(position)){
//					 cu = true;
//				}else{
//					 cu = true;
//				}


                    // 先将所有的置为FALSE
                    for (Integer p : isSelected.keySet()) {
                        isSelected.put(p, false);
                    }
                    // 再将当前选择CB的实际状态
                    isSelected.put(position + 1, cu);
//                for(Red_CouponBean a:list){
//                	a.setIscheck(false);
//                }


                    ok = 1;

                    for (int i = 0; i < list.size(); i++) {
                        list.get(i).setIscheck(false);
                    }
                    list.get(position - 1).setIscheck(cu);
                    adapter.notifyDataSetChanged();
                    beSelectedData.clear();
                    if (cu) beSelectedData.add(list.get(position - 1));

//                if(list.get(position).isIscheck()){
//                	 btn_confirm.setBackgroundResource(R.drawable.my_button);
//                     btn_confirm.setEnabled(true);
//                     kedian=1;
                    for (Red_CouponBean a : list) {
                        if (a.isIscheck()) {
                            coupon_id = a.getId();
                            rateadd = a.getRate();
                            name = a.getKindName();
                            date = a.getEndDate();
                            add_type = a.getType();
                            getMoney = a.getRate();
                            if (a.getType().equals("INTEREST_RATES")) {

                                maxAmount = Integer.parseInt(a.getMaxAmount());
                                minAmount = Integer.parseInt(a.getMinAmount());
                            }
                            if (a.getType().equals("REBATE")) {
//                                intentmoney = String.valueOf(getIntent().getIntExtra("amount", 0) - Integer.valueOf(list.get(position-1).getBalance()));
//                                intentmoney = String.valueOf(Integer.valueOf(list.get(position - 1).getBalance()));
                                intentmoney = String.valueOf(list.get(position-1).getBalance());
                                intentTitle = list.get(position - 1).getKindName();
                                zhekouMoney = list.get(position - 1).getBalance();

                                touzijine = list.get(position - 1).getMinAmount();

                            } else {
                                intentmoney = "0";
                            }

                            red_type = a.getCashbackType();
                            availableGiftAmount = a.getRemainBalance();
                        }
//        				else {
//        					coupon_id="";
//        					rateadd="";
//        					name="";
//        					date="";
//        					add_type="";
//        					red_type="";
//        					availableGiftAmount="";
//        				}
                    }
                    Log.e("coupon_id", coupon_id + "---");
                    indataIntent();
//                }else{
//                	btn_confirm.setEnabled(false);
//                	 kedian=0;
//                	btn_confirm.setBackgroundResource(R.drawable.btn_unclick);
//                	
//                }

                    Log.e("add_type", add_type);


                }
            }
        });

//		initData();
        initData2("byDate", "0");

    }

    String intentmoney = "0";

    String cardId = "";

    private void indataIntent() {
        Intent intent = new Intent();

        if (add_type.equals("GIFT_CARD")) {
            //红包
            intent.putExtra("cardId", cardId);
            intent.putExtra("my_flag", "1");
            intent.putExtra("add_type", add_type);
            intent.putExtra("id", coupon_id);
            intent.putExtra("rate", rateadd);
            intent.putExtra("name", name);
            intent.putExtra("date", date);
            intent.putExtra("red_money", "0.00");
            intent.putExtra("red_type", red_type);
            intent.putExtra("availableGiftAmount", availableGiftAmount);
            intent.putExtra("add_type", add_type);
            intent.putExtra("touzijine", touzijine);
            intent.putExtra("add_type", true);
        } else if (add_type.equals("REBATE")) {
//折扣券
            cardId = coupon_id;
            intent.putExtra("my_flag", "2");
            intent.putExtra("cardId", cardId);
            intent.putExtra("getMoney", getMoney);
            intent.putExtra("add_type", add_type);
            intent.putExtra("maxAmount", maxAmount);
            intent.putExtra("minAmount", minAmount);
            intent.putExtra("id", coupon_id);
            intent.putExtra("cardId", coupon_id);
            intent.putExtra("intentTitle", intentTitle);
            intent.putExtra("rate", rateadd);
            intent.putExtra("name", name);
            intent.putExtra("date", date);
            intent.putExtra("red_type", add_type);
            intent.putExtra("red_money", "0.00");
            intent.putExtra("red", false);
            intent.putExtra("intentmoney", intentmoney);
            intent.putExtra("zhekouMoney", zhekouMoney);
            intent.putExtra("touzijine", touzijine);

//            cardId
        } else {
            intent.putExtra("my_flag", "3");
            intent.putExtra("cardId", cardId);
            intent.putExtra("add_type", add_type);
            intent.putExtra("maxAmount", maxAmount);
            intent.putExtra("minAmount", minAmount);
            intent.putExtra("id", coupon_id);
            intent.putExtra("rate", rateadd);
            intent.putExtra("name", name);
            intent.putExtra("date", date);
            intent.putExtra("red_type", add_type);
            intent.putExtra("red_money", "0.00");
            intent.putExtra("red", false);
            intent.putExtra("touzijine", touzijine);
        }
        setResult(2, intent);
        Editor editor = sha.edit();
        editor.putString("check", coupon_id);

        editor.commit();
        finish();
    }

    private void initData2(String sortType, String sortFlag) {
        // TODO Auto-generated method stub
        AjaxParams params = new AjaxParams();
        params.put("productId", id);
        params.put("sortType", sortType);
        params.put("sortFlag", sortFlag);
        VolleryUtil volleryUtil = new VolleryUtil() {

            @Override
            public void onUpDate(JSONObject jsonObject) {
                // TODO Auto-generated method stub
                Log.e("red_coupon", jsonObject.toString());

                Log.d(jsonObject.toString(), "233333333333");
                Log.d(jsonObject.toString(), "233333333333");
                Log.d(jsonObject.toString(), "233333333333");
                if (jsonObject.optInt("success") == 1) {

                    Gson gson = new Gson();


                    list = gson.fromJson(jsonObject.optString("resultList"), new TypeToken<ArrayList<Red_CouponBean>>() {
                    }.getType());


                    adapter = new UseCoupon(list, Investcoupon.this);
                    lv.setAdapter(adapter);
                    if (list.size() < 1) {
                        lv.setVisibility(View.GONE);
                        img_none_list.setVisibility(View.VISIBLE);
                    } else {
                        lv.setVisibility(View.VISIBLE);
                        img_none_list.setVisibility(View.GONE);
                    }
                    if (sha.getString("check", "").equals("")) {
                        for (Red_CouponBean a : list) {
                            a.setIscheck(false);
                        }
                        btn_confirm.setEnabled(true);
                        btn_confirm.setBackgroundResource(R.drawable.btn_unclick);


                    } else {
                        btn_confirm.setVisibility(View.VISIBLE);
                        btn_confirm.setEnabled(true);
                        btn_confirm.setBackgroundResource(R.drawable.v6button);
                        if (sha.getString("check", "").equals("red")) {
                            ck_red.setChecked(true);
                        } else {
                            for (Red_CouponBean a : list) {
                                if (sha.getString("check", "").equals(a.getId())) {
                                    a.setIscheck(true);
                                }
                            }
                        }

                    }
                    if (list == null || list.size() == 0)
                        return;
                    if (isSelected != null)
                        isSelected = null;
                    isSelected = new HashMap<Integer, Boolean>();
                    for (int i = 0; i < list.size(); i++) {
                        isSelected.put(i, false);
                    }
                    // 清除已经选择的项
                    if (beSelectedData.size() > 0) {
                        beSelectedData.clear();
                    }
                    adapter.notifyDataSetChanged();

                } else {
                    if (jsonObject.optString("comment").equals("please login")) {
                        if (jsonObject.optString("comment").equals("please login")) {
//							Intent intent = new Intent(Investcoupon.this,
//									V6LoginActivity.class);
//							intent.putExtra("close", 1);
//							startActivity(intent);
                            mpleaseLogin(Investcoupon.this, 1, 0, 0);
                        }
                    } else {
                        Toast.makeText(Investcoupon.this, jsonObject.optString("comment"), 2000).show();
                    }

                }
            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub

            }
        };
        volleryUtil.setHeaders("token", getToken());
//        volleryUtil.setHeaders("deviceId", getdeviceId());
        volleryUtil.setRequestMethod(0);
        volleryUtil.setShow(true);
//        api/coupon/getUserCouponsNew/{product}

//	    volleryUtil.execute(Investcoupon.this, Constant.URL+"/api/coupon/getUserCoupons/"+type+"?flagFresh="+flag, "&"+params.toString());
        volleryUtil.execute(Investcoupon.this, Constant.URL + "/api/coupon/getUserCouponsNew/" + type + "?flagFresh=" + flag, "&" + params.toString());


    }

    private void initData() {
        AjaxParams params = new AjaxParams();
        params.put("flagFresh", isnew);
        params.put("flagRedpack", isred);
        // TODO Auto-generated method stub
        VolleryUtil volleryUtil = new VolleryUtil() {

            @Override
            public void onUpDate(JSONObject jsonObject) {
                // TODO Auto-generated method stub


                if (jsonObject.optInt("success") == 1) {
                    Log.e("直投", jsonObject.toString());
                    Gson gson = new Gson();
                    JSONObject json = jsonObject.optJSONObject("data");
                    if (!jsonObject.optString("data").equals("null")) {
                        list = gson.fromJson(json.optString("raisingRatesList"), new TypeToken<ArrayList<Mycoupon>>() {
                        }.getType());

                    }

                    adapter = new UseCoupon(list, Investcoupon.this);
                    lv.setAdapter(adapter);
                    if (sha.getString("check", "").equals("")) {
                        for (Red_CouponBean a : list) {
                            a.setIscheck(false);
                        }
                    } else {
                        if (sha.getString("check", "").equals("red")) {
                            ck_red.setChecked(true);
                        } else {
                            for (Red_CouponBean a : list) {
                                if (sha.getString("check", "").equals(a.getId())) {
                                    a.setIscheck(true);

                                }
                            }
                        }

                    }
                    if (list == null || list.size() == 0)
                        return;
                    if (isSelected != null)
                        isSelected = null;
                    isSelected = new HashMap<Integer, Boolean>();
                    for (int i = 0; i < list.size(); i++) {
                        isSelected.put(i, false);
                    }
                    // 清除已经选择的项
                    if (beSelectedData.size() > 0) {
                        beSelectedData.clear();
                    }
                    adapter.notifyDataSetChanged();

                } else {
                    if (jsonObject.optString("comment").equals("please login")) {
                        if (jsonObject.optString("comment").equals("please login")) {
                            Intent intent = new Intent(Investcoupon.this,
                                    LoginActivity.class);
                            intent.putExtra("close", 1);
                            startActivity(intent);
                        }
                    } else {
                        Toast.makeText(Investcoupon.this, jsonObject.optString("comment"), 2000).show();
                    }
                }
            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub

            }
        };
        volleryUtil.setHeaders("token", getToken());
        volleryUtil.setRequestMethod(0);
        volleryUtil.setShow(true);
        volleryUtil.execute(Investcoupon.this, Constant.URL + "/api/v5/product/" + id + "/RaisingratesList?", params.toString());

    }


    String touzijine = "0";

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub


//			Intent intent2=new Intent();
//			setResult(1, intent2); 
        finish();


        return super.onKeyDown(keyCode, event);
    }

    public class UseCoupon extends BaseAdapter {
        private List<Red_CouponBean> list;
        private Context context;
        private DecimalFormat df = new DecimalFormat("###0.00");
        private DecimalFormat df2 = new DecimalFormat("###0.0");
        private Dialog dialog;
        private SharedPreferences sha;

        public UseCoupon(List<Red_CouponBean> list, Context context) {
            super();
            this.list = list;
            this.context = context;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @SuppressLint("NewApi")
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            Help h = null;

            if (convertView == null) {
                h = new Help();
                try {

                    convertView = LayoutInflater.from(context).inflate(R.layout.coupon_item, null);
                } catch (OutOfMemoryError e) {
                    // TODO: handle exception
                }


                h.rate_or_remain = (TextView) convertView.findViewById(R.id.rate);
                h.tv_reward_name = (TextView) convertView.findViewById(tv_reward_name);
                h.tv_coupon_name = (TextView) convertView.findViewById(R.id.coupon_name);
                h.li_bg = (LinearLayout) convertView.findViewById(R.id.li_bg);
                h.ck = (CheckBox) convertView.findViewById(R.id.ck);
                h.tv_time = (TextView) convertView.findViewById(R.id.end_time);
                h.li_ground = (LinearLayout) convertView.findViewById(R.id.li_ground);
                h.invest_ground = (TextView) convertView.findViewById(R.id.invest_ground);
                h.use_ground = (TextView) convertView.findViewById(R.id.use_ground);
                h.baifen = (TextView) convertView.findViewById(R.id.baifen);
                h.img_get = (ImageView) convertView.findViewById(R.id.img_get);


                h.li_coupon = (LinearLayout) convertView.findViewById(R.id.li_coupon);


                convertView.setTag(h);
            } else {
                h = (Help) convertView.getTag();

            }
            h.li_ground.setVisibility(View.VISIBLE);
            if (list.get(position).getType().equals("GIFT_CARD")) {

                h.img_get.setVisibility(View.GONE);
                h.baifen.setVisibility(View.GONE);
                h.tv_reward_name.setText(list.get(position).getKindName() + Regular.getDecimalFormatTwo(Double.parseDouble(list.get(position).getBalance())) + "元");

                h.tv_coupon_name.setText("剩余金额(元)");

                h.rate_or_remain.setText(df.format(Double.parseDouble(list.get(position).getRemainBalance())));
//                h.rate_or_remain.setText(list.get(position).getRemainBalance());
                h.tv_time.setText("有效期至：" + Regular.getMilliToDate3(list.get(position).getEndDate()));

                h.li_bg.setBackground(new BitmapDrawable(BitmapUtils.readBitMap(context, R.drawable.v6red_bg)));
//				h.li_bg.setBackgroundResource(R.drawable.redbg);
//				h.li_bg.setBackground(new BitmapDrawable(BitmapUtils.readBitMap(context, R.drawable.zhekouquan)));
                h.use_ground.setVisibility(View.VISIBLE);
                h.invest_ground.setText("返利比例：年化" + list.get(position).getRate() + "%");
                if (list.get(position).getCashbackType().equals("PERCENTAGE")) {
//					h.use_ground.setText("返现比例:"+"投资金额*"+list.get(position).getRate()+"%"+"("+"已使用"+list.get(position).getUseBalance()+"元)");	
                    h.use_ground.setText("适用项目：" + list.get(position).getProductAllowNames());
                } else {
//					h.tv_use_ground.setText("返现比例:"+"年化"+list.get(position).getRate()+"%"+"("+"已使用"+list.get(position).getUseBalance()+"元)");	
                    h.use_ground.setText("适用项目：" + list.get(position).getProductAllowNames());
                }


            } else if (list.get(position).getType().equals("INTEREST_RATES")) {
                h.img_get.setVisibility(View.GONE);

                h.li_ground.setVisibility(View.VISIBLE);
                h.baifen.setVisibility(View.VISIBLE);


//				h.baifen.setTextColor(Color.parseColor("#fff"));

//				h.li_bg.setBackgroundResource(R.drawable.coupon);
                h.li_bg.setBackground(new BitmapDrawable(BitmapUtils.readBitMap(context, R.drawable.v6couon_bg)));
//				h.li_bg.setBackground(new BitmapDrawable(BitmapUtils.readBitMap(context, R.drawable.zhekouquan)));
                h.tv_reward_name.setText(list.get(position).getKindName());

                h.invest_ground.setText(list.get(position).getMinAmount() + "元≤投资金额≤" + list.get(position).getMaxAmount() + "元");
//				h.tv_reward_name.setText("加息券");
                h.tv_coupon_name.setText("加息券");
                h.rate_or_remain.setText(df2.format(Double.parseDouble(list.get(position).getRate())));

//                h.rate_or_remain.setText(df2.format(Double.parseDouble(list.get(position).getRate())));
//                h.rate_or_remain.setText(list.get(position).getRate());
                if (list.get(position).getDays() == null || list.get(position).getDays().equals("0") || list.get(position).getDays().equals("null")) {
                    h.use_ground.setText("适用项目：" + list.get(position).getProductAllowNames());
                } else {
                    h.use_ground.setText("适用项目：" + list.get(position).getProductAllowNames() + "  (" + list.get(position).getDays() + "天)");
                }

                h.tv_time.setText("有效期至：" + Regular.getMilliToDate3(list.get(position).getEndDate()));

            } else {

                h.img_get.setVisibility(View.GONE);

                h.li_ground.setVisibility(View.VISIBLE);
                h.baifen.setVisibility(View.VISIBLE);
                h.baifen.setText("元");

//				h.baifen.setTextColor(Color.parseColor("#fff"));

//				h.li_bg.setBackgroundResource(R.drawable.coupon);
//				h.li_bg.setBackground(new BitmapDrawable(BitmapUtils.readBitMap(context,R.drawable.v6couon_bg)));
                h.li_bg.setBackground(new BitmapDrawable(BitmapUtils.readBitMap(context, R.drawable.zhekouquan)));
                h.tv_reward_name.setText(list.get(position).getKindName());
                h.tv_reward_name.setVisibility(View.GONE);
//				h.invest_ground.setText(list.get(position).getMinAmount()+"元≤投资金额≤"+list.get(position).getMaxAmount()+"元");
//				h.tv_reward_name.setText("加息券");
//				h.tv_coupon_name.setText("加息券");


                h.tv_coupon_name.setText(list.get(position).getKindName());
                h.invest_ground.setText("投资金额：" + "≥" + list.get(position).getMinAmount() + "元");

//                h.rate_or_remain.setText(df2.format(Double.parseDouble(list.get(position).getRate())));
                h.rate_or_remain.setText(list.get(position).getBalance());
                if (list.get(position).getDays() == null || list.get(position).getDays().equals("0") || list.get(position).getDays().equals("null")) {
                    h.use_ground.setText("适用项目：" + list.get(position).getProductAllowNames());
                } else {
                    h.use_ground.setText("适用项目：" + list.get(position).getProductAllowNames() + "  (" + list.get(position).getDays() + "天)");
                }

                h.tv_time.setText("有效期至：" + Regular.getMilliToDate3(list.get(position).getEndDate()));

            }
            if (myid.equals(list.get(position).getId()) && ok != 1) {
                boolean cu = true;

                // 先将所有的置为FALSE  
                for (Integer p : isSelected.keySet()) {
                    isSelected.put(p, false);
                }
                // 再将当前选择CB的实际状态  
                isSelected.put(position, cu);
                list.get(position).setIscheck(isSelected.get(position));

            }
            if (list.get(position).isIscheck()) {
                h.ck.setVisibility(View.VISIBLE);
            } else {
                h.ck.setVisibility(View.GONE);
            }
            h.ck.setChecked(list.get(position).isIscheck());

            return convertView;
        }

        class Help {

            private LinearLayout li_bg, li_ground, li_coupon;
            private View zhanwei, zhanwei2;
            private TextView tv_reward_name;//红包名字
            private TextView tv_coupon_name;//加息券名字
            private TextView rate_or_remain;//利率或者名字
            private TextView baifen;
            private TextView tv_time;//道具到期
            private TextView invest_ground;//投资范围
            private TextView use_ground;//适用范围
            private ImageView img_get;
            private CheckBox ck;
        }


    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.img_back:


//				Intent intent2=new Intent();
//				setResult(1, intent2); 
                finish();

                break;

            case R.id.btn_confirm:

                Intent intent4 = new Intent();
                setResult(1, intent4);
                Editor editor = sha.edit();
                editor.putString("check", "");
                editor.putString("type", "");
                editor.commit();
                finish();


                break;
            case R.id.rl_back:
//if(kedian==0){

//				Intent intent3=new Intent();
//				setResult(1, intent3); 
                finish();
//			}else{
//				finish();
//			}
                break;
            case R.id.li_amount:
                if (list.size() > 0) {


                    if (li_amount.isSelected()) {
                        Collections.reverse(list);
                        adapter.notifyDataSetChanged();
                        if (imgamount == 0) {
                            img_amount.setImageResource(R.drawable.last_sort);
                            imgamount = 1;
                        } else {
                            img_amount.setImageResource(R.drawable.index_sort);
                            imgamount = 0;
                        }

                    } else {
                        img_amount.setImageResource(R.drawable.index_sort);
                        imgamount = 0;
                        img_data.setImageResource(R.drawable.null_sort);
                        initData2("byRate", "1");
                    }
                    text_amount.setTextColor(Color.parseColor("#64A6FE"));
                    text_data.setTextColor(Color.parseColor("#9a9a9a"));
                    li_amount.setSelected(true);
                    li_data.setSelected(false);
                }
                break;
            case R.id.li_data:
                if (list.size() > 0) {


                    if (li_data.isSelected()) {
                        Collections.reverse(list);
                        adapter.notifyDataSetChanged();
                        if (imgdata == 0) {
                            img_data.setImageResource(R.drawable.index_sort);
                            imgdata = 1;
                        } else {
                            img_data.setImageResource(R.drawable.last_sort);
                            imgdata = 0;
                        }
                    } else {
                        img_data.setImageResource(R.drawable.index_sort);
                        imgdata = 0;
                        img_amount.setImageResource(R.drawable.null_sort);
                        initData2("byDate", "1");
                    }
                    text_data.setTextColor(Color.parseColor("#64A6FE"));
                    text_amount.setTextColor(Color.parseColor("#9a9a9a"));
                    li_amount.setSelected(false);
                    li_data.setSelected(true);
                }
                break;
        }


    }

}
